from pydantic import Field, field_validator

from .base import Base
from .resume_id import ResumeSalaryId

LENGTH_SALARY = 2


class BaseResumeSalary(Base):
    salary: str = Field(..., description='Зарплата')

    @field_validator('salary')
    @classmethod
    def check_salary(cls, salary: str) -> str:
        salary_arr = salary.split()
        msg = 'Запрлата указана неверно!'
        if LENGTH_SALARY > len(salary_arr) or len(salary_arr) > LENGTH_SALARY:
            raise ValueError(msg)
        try:
            int(salary_arr[0])
            return salary
        except ValueError:
            raise ValueError(msg)


class ResumeSalaryCreate(BaseResumeSalary):
    pass


class ResumeSalaryResult(BaseResumeSalary):
    id: ResumeSalaryId = Field(..., description='Уникальный индентификатор')


class ResumeSalaryUpdate(BaseResumeSalary):
    salary: str | None = Field(None, description='Зарплата')

    @field_validator('salary')
    @classmethod
    def check_salary(cls, salary: str | None) -> str | None:
        if salary is None:
            return salary
        return super().check_salary(salary)
